package com.xx.wars.screen;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import com.xx.framework.gl.Camera2D;
import com.xx.framework.gl.SpriteBatcher;
import com.xx.framework.impl.GLGraphics;
import com.xx.wars.node.SpriteNode;
import com.xx.wars.util.CloneUtil;
import com.xx.wars.util.GameSetting;
import com.xx.wars.util.WindowManager;

public class WorldRenderer {
	
	public 	Camera2D cam;
	
	public  float blueValue = 1.0f;
	public  boolean changeBackground;
	private boolean addColor;
	
	private World world;
	private SpriteBatcher batcher;
	private GLGraphics glGraphics;

	public WorldRenderer(GLGraphics glGraphics, SpriteBatcher batcher,
			World world) {
		this.glGraphics = glGraphics;
		this.world = world;
		this.cam = new Camera2D(glGraphics, GameSetting.SCREEN_WIDTH,
				GameSetting.SCREEN_HEIGHT);
		this.batcher = batcher;
	}

	public void render() {
		renderBackground();
		renderObjects();
	}

	public void renderBackground() {
		if(addColor && WindowManager.gameScreen.state == GameScreen.GAME_READY){
			blueValue += 0.005;
		}
		
		if(!addColor && WindowManager.gameScreen.state == GameScreen.GAME_RUNNING){
			blueValue -= 0.005;
		}
		
		if(blueValue >= 1.0f){
			addColor = false;
		}
		if(blueValue <= 0.5f){
			addColor = true;
		}
		GL10 gl = glGraphics.getGL();
		gl.glClearColor(blueValue, blueValue, blueValue, 1.0f);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
	}

	public void renderObjects() {
		renderMonsters();
	}
	private void renderMonsters() {
		List<SpriteNode> monstersClone = CloneUtil.clonSpriteNode(world.elements);
		int len = monstersClone.size();
//		for (int i = 0; i < len; i++) {
//			SpriteNode node = (SpriteNode)monstersClone.get(i);
//			node.presentHostLine(batcher,cam,glGraphics.getGL());
//		}
		for (int i = 0; i < len; i++) {
			SpriteNode node = (SpriteNode)monstersClone.get(i);
			node.present(batcher,cam,glGraphics.getGL());
		}
	}
}
